package com.taobao.tddl;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

import com.google.common.base.Charsets;
import com.google.common.hash.HashCode;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import com.taobao.tddl.client.sequence.exception.SequenceException;
import com.taobao.tddl.client.sequence.impl.GroupSequence;
import com.taobao.tddl.dao.HaqialongDao;
import com.taobao.tddl.dao.TddlSampleDao;
import com.taobao.tddl.entity.Haqiaolong;
import com.taobao.tddl.entity.TddlSample;

public class TddlDemo {

	public static void main(String[] args) throws SequenceException,
			InterruptedException {

		ExecutorService executorService = Executors.newFixedThreadPool(100);
		// ApplicationContext context = new ClassPathXmlApplicationContext(new
		// String[]{"classpath:spring-context-ibatis.xml"});
		// ApplicationContext context = new ClassPathXmlApplicationContext(new
		// String[]{"classpath:spring-context-tddl.xml"});
		// ApplicationContext context = new ClassPathXmlApplicationContext(new
		// String[] { "classpath:spring-context-commerceitem-tddl.xml" });
		ApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "classpath:spring-context-tddl.xml" });
		final JdbcTemplate tddlDS = (JdbcTemplate) context.getBean("tddlDS");
		// insertData(tddlDS);
		// for (int i = 0; i < 1000000; i++) {
		// final int index = i;
		// executorService.submit(new Runnable() {
		//
		// @Override
		// public void run() {
		// select1(index, tddlDS);
		// }
		// });
		// }
		// TddlSampleDao tddlSampleDao =
		// (TddlSampleDao)context.getBean("tddlSampleDao");
		//
		// for (int i = 0; i < 100000; i++) {
		// tddlSampleDao.insert(new TddlSample(i, i + ""));
		// }
		// GroupSequence groupSequence = (GroupSequence)
		// context.getBean("sequence");
		// HaqialongDao haqialongDao = (HaqialongDao)
		// context.getBean("haqialongDao");
		//
		// Haqiaolong haqiaolong = null;
		// for (int i = 0; i < 500000; i++) {
		// haqiaolong = new Haqiaolong();
		// haqiaolong.setName("abc");
		// haqiaolong.setId(groupSequence.nextValue());
		// haqialongDao.insert(haqiaolong);
		// System.out.println("新增数据");
		// }

		// Haqiaolong haqiaolong = new Haqiaolong();
		// haqiaolong.setId(486);
		// haqiaolong.setName("111");
		// System.out.println(haqialongDao.select(haqiaolong));

		// System.out.println(haqialongDao.queryAll());
		// haqialongDao.selectBydirect();
		// TddlSampleDao tddlSampleDao = (TddlSampleDao)
		// context.getBean("tddlSampleDao");
		//
		for (int i = 0; i < 100000; i++) {
			select1(i, tddlDS);
		}
	}

	public static void insert1(int id, JdbcTemplate tddlDS) {
		String sql = "insert into tddl_shard VALUES (" + id + "," + id
				+ ",now());";
		System.out.println(tddlDS.update(sql));
	}

	public static void select1(int id,JdbcTemplate tddlDS) {
		// String createSql = "/*+TDDL";
		// createSql += "({'type':'direct','dbid':'haqiaolong_group_0'})";
		// createSql +=
		// "*/ CREATE TABLE IF NOT EXISTS `migration_commerceitem_1008` ("
		// + "`id` bigint(20) NOT NULL AUTO_INCREMENT," +
		// "`user_Id` varchar(50) NOT NULL COMMENT '用户id',"
		// + "`site_Id` varchar(50) NOT NULL COMMENT '站点id',"
		// + "`commerce_Item_Id` varchar(50) DEFAULT NULL COMMENT '商品项ID',"
		// + "`biz_Type` varchar(50) DEFAULT 'aaa' COMMENT '业务类型',"
		// + "`product_Id` varchar(50) DEFAULT NULL COMMENT '商品ID',"
		// + "`sku_Id` varchar(50) DEFAULT NULL COMMENT 'skuId',"
		// + "`quantity` int(11) DEFAULT NULL COMMENT '数量',"
		// +
		// "`sale_Promotion_Id` varchar(50) DEFAULT NULL COMMENT '促销对象ID（搭配购，团购，抢购，预售）',"
		// +
		// "`main_Item_Id` varchar(50) DEFAULT NULL COMMENT '主商品commerceItemID',"
		// + "`create_time` timestamp NOT NULL," + "PRIMARY KEY (`id`),"
		// + "KEY `idx_mig_com_user_id_1008` (`user_Id`)," +
		// "KEY `idx_mig_com_site_id_1008` (`site_Id`)"
		// + ") ENGINE=InnoDB AUTO_INCREMENT=1757131 DEFAULT CHARSET=utf8";
//		String sql = "SELECT * FROM tddl_shard where id =" + id;
//		System.out.println(tddlDS.queryForList(sql));
		String sql1 = "select * from  tddl_shard where id = " + id;
		System.out.println(tddlDS.queryForList(sql1));

	}

	private static void insertData(JdbcTemplate tddlJT) {
		for (int i = 20; i < 30; i++) {
			String sql = "insert into email_hash (ID,HASHCODE,EMAIL,USER_ID,DBNAME) values (?,?,?,?,?)";
			String email = "haqiaolong" + i + "@126.com";
			System.out.println(hash(email));
			Object[] arguments = new Object[] { i, hash(email), email, i,
					"profile" };
			tddlJT.update(sql, arguments);
		}

	}

	private static long hash(String value) {
		HashFunction hf = Hashing.md5();
		HashCode hc = hf.newHasher().putString(value, Charsets.UTF_8).hash();
		long hash = hc.asLong();
		if (hash < 0)
			hash = -hash;
		return hash;
	}
}
